AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

local Ground = 1 + 0 + 2 + 8 + 32

function ENT:Initialize()
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	local phys = self.Entity:GetPhysicsObject()
	self.damaged = 0
	self.product = 0 --should work :S
	

	self.timer = CurTime() +  1
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMass(10)

	end
	
end

function ENT:Damage()
	if (self.damaged == 0) then
		self.damaged = 1
	end
end

function ENT:Repair()
	self.Entity:SetColor( 255, 255, 255, 255 )
	self.health = self.max_health
	self.damaged = 0
end

function ENT:Destruct()
	local Effect = EffectData()
		Effect:SetOrigin(self.Entity:GetPos())
		Effect:SetScale(1)
		Effect:SetMagnitude(25)
		util.Effect("Explosion", Effect, true, true)

		self.Entity:Remove()
end


function ENT:Output()
	return 1
end

function ENT:Use()
end


function ENT:Think()
	if (self.damaged == 1) then
		self:SetOverlayText( "Plant\n(needs watering)" )
	else
		self:SetOverlayText( "Plant" )
	end
	if ( self.timer > CurTime() ) then return end
	local found = 0
	local tAllPlayers = player.GetAll()
	for _, pPlayer in pairs( tAllPlayers ) do
		if pPlayer.Entity:GetPos():Distance(self.Entity:GetPos()) < self.range then
			found = 1
			break
		end
	end
	DamageLS(self.Entity, 1)
	if (found == 1) then self.product = 1 end
	self.timer = CurTime() +  1
end


function ENT:AcceptInput(name,activator,caller)
	if name == "Use" and caller:IsPlayer() and caller:KeyDownLast(IN_USE) == false then
		self:Repair()
	end
end
